package com.mz.ozy.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.activity.me.JoinMemberActivity;
import com.mz.ozy.activity.me.UpVipActivity;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.AppManager;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.LanguageUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.LoadingDialog;
import com.mz.ozy.widget.dialog.AlertDialog;
import com.mz.ozy.widget.dialog.NotVipDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.lang.reflect.ParameterizedType;

import androidx.annotation.Nullable;

/**
 * base
 */
public abstract class BaseActivity<V extends BaseView, P extends BasePresenterImpl<V>> extends RxFragmentActivity implements BaseView {

    protected View titleBar, ll_top;
    protected ImageView tvBack;
    protected TextView tvTitle;
    protected TextView tvCenterTitle;

    protected TextView rightTextView;
    protected TextView leftTextView;

    protected ImageView rightImage;
    public P mPresenter;
    LoadingDialog kProgressHUD;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //6.0系统及以上状态栏字体，图标颜色变成黑色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        AppManager.getAppManager().addActivity(this);
        kProgressHUD = new LoadingDialog(this);
        mPresenter = getInstance(this, 1);
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        LanguageUtils.switchLanguage(this);


    }

    //
    public void showVipDialog() {
        NotVipDialog dialog = new NotVipDialog(this);
        dialog.showDialog();
    }


    @Override
    public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
        if (checkDoubleClick(intent)) {
            super.startActivityForResult(intent, requestCode, options);

        }
    }

    private String mActivityJumpTag;        //activity跳转tag
    private long mClickTime;                //activity跳转时间

    /**
     * 检查是否重复跳转，不需要则重写方法并返回true
     */
    protected boolean checkDoubleClick(Intent intent) {

        // 默认检查通过
        boolean result = true;
        // 标记对象
        String tag;
        if (intent.getComponent() != null) { // 显式跳转
            tag = intent.getComponent().getClassName();
        } else if (intent.getAction() != null) { // 隐式跳转
            tag = intent.getAction();
        } else {
            return true;
        }

        if (tag.equals(mActivityJumpTag) && mClickTime >= SystemClock.uptimeMillis() - 500) {
            // 检查不通过
            result = false;
        }

        // 记录启动标记和时间
        mActivityJumpTag = tag;
        mClickTime = SystemClock.uptimeMillis();
        return result;
    }

    /**
     * 设置沉浸式状态栏时，要把标题栏往下移动状态栏的高度
     */
    protected void setTitleBarMargin() {
        int length = AppUtils.getStatusBarHeight(this);
        //注意布局是哪种Layout
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(titleBar.getLayoutParams());
        lp.setMargins(0, length, 0, 0);
        titleBar.setLayoutParams(lp);
    }


    public void initTitle() {
        titleBar = findViewById(R.id.titleBar);
        tvBack = (ImageView) findViewById(R.id.back);
        tvTitle = (TextView) findViewById(R.id.title);
        tvCenterTitle = findViewById(R.id.tv_center_title);
        rightTextView = (TextView) findViewById(R.id.right);
        leftTextView = (TextView) findViewById(R.id.leftView);
        rightImage = (ImageView) findViewById(R.id.rightImage);
        tvBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }

    public void initTitle3() {
        titleBar = findViewById(R.id.titleBar);
        tvBack = (ImageView) findViewById(R.id.back);
        tvTitle = (TextView) findViewById(R.id.title);
        tvCenterTitle = findViewById(R.id.tv_center_title);
        rightTextView = (TextView) findViewById(R.id.right);
        leftTextView = (TextView) findViewById(R.id.leftView);
        rightImage = (ImageView) findViewById(R.id.rightImage);
    }

    public void initTitle2() {
        QMUIStatusBarHelper.translucent(this);
        titleBar = findViewById(R.id.titleBar);
        tvBack = (ImageView) findViewById(R.id.back);
        tvTitle = (TextView) findViewById(R.id.title);
        tvCenterTitle = findViewById(R.id.tv_center_title);
        rightTextView = (TextView) findViewById(R.id.right);
        leftTextView = (TextView) findViewById(R.id.leftView);
        rightImage = (ImageView) findViewById(R.id.rightImage);
        ll_top = findViewById(R.id.ll_top);

        titleBar.setBackgroundResource(android.R.color.transparent);
        //   tvCenterTitle.setTextColor(getResources().getColor(R.color.white));
        //  tvBack.setImageResource(R.mipmap.white_back_icon);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        if (kProgressHUD != null) {
            kProgressHUD.dismissLoading();
            kProgressHUD = null;
        }
    }

    //反射获取当前Presenter对象
    public <M> M getInstance(Object o, int i) {
        try {
            return ((Class<M>) ((ParameterizedType) (o.getClass().getGenericSuperclass())).getActualTypeArguments()[i]).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public void showLoading() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (kProgressHUD.isShow()) {
                    return;
                }
                kProgressHUD.showLoading();
            }
        });

    }

    @Override
    public void dismissLoading() {
        if (kProgressHUD != null && kProgressHUD.isShow()) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    kProgressHUD.dismissLoading();
                }
            });
        }

    }

    //统一处理请求失败业务
    public void handleFailResponse(NetBean bean) {
        if (bean.getRespCode().equals(Constants.APP_NOT_INIT)) {
            AppManager.getAppManager().goLoginActivity(this);

        } else if (bean.getRespCode().equals("1138")) {//未购买会员
            showAlertDialog();

        } else {
            ToastUtils.showToast(bean.getRespMsg());
        }
    }

    public void showAlertDialog() {
        AlertDialog dialog = new AlertDialog(this);
        dialog.setOnDialogListener(new AlertDialog.onDialogListener() {
            @Override
            public void onConfirm() {
//                startActivity(new Intent(BaseActivity.this, UpVipActivity.class));
                startActivity(new Intent(BaseActivity.this, JoinMemberActivity.class));
            }
        });
        dialog.toggleDialog();
    }

}
